package io.bsly.modules.sys.controller;

import io.bsly.common.utils.PageUtils;
import io.bsly.common.utils.R;
import io.bsly.modules.app.entity.localEntity.DyHotelProductEntity;
import io.bsly.modules.app.pojo.HotelProductFrom;
import io.bsly.modules.app.service.localmaster.DyHotelProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 酒店和产品关联
 * @author fys
 */
@Api(tags = "后台商品管理")
@Validated
@RestController
@RequestMapping("sys/hotelproduct")
public class SysHotelProductController {
    @Autowired
    private DyHotelProductService dyHotelProductService;

    /**
     * 品名对应的酒店分页
     */
    @ApiOperation("品名酒店分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hotelId",value = "酒店id" , dataType = "int", paramType = "body"),
            @ApiImplicitParam(name = "productId",value = "产品id" , dataType = "int", paramType = "body"),
            @ApiImplicitParam(name = "hotelName",value = "酒店名称" , dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "hotelAddr",value = "酒店地址" , dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "page",value = "当前页数" , dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "limit",value = "每页条数" , dataType = "string", paramType = "body")
    })
    @PostMapping("/list")
    public R list(@RequestBody @Valid HotelProductFrom from){
        PageUtils page = dyHotelProductService.queryPage(from);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation("根据ID查询")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		DyHotelProductEntity dyHotelProduct = dyHotelProductService.getById(id);
        return R.ok().put("dyHotelProduct", dyHotelProduct);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存关联关系",notes = "保存或更新，带有id为更新")
    @PostMapping("/saveRelation")
    public R save(@RequestBody @Valid DyHotelProductEntity dyHotelProduct){
        try {
            dyHotelProductService.saveOrUpdate(dyHotelProduct);
        }catch (DuplicateKeyException e){
            throw new DuplicateKeyException("HotelId为："+dyHotelProduct.getHotelId()+"  ProductId为"+ dyHotelProduct.getProductId());
        }
        return R.ok();
    }

    /**
     * 批量保存
     */
    @ApiOperation(value = "批量保存",notes = "批量保存关联关系")
    @PostMapping("/saveBatch")
    public R saveBatch(@RequestBody @Valid List<DyHotelProductEntity> list){
        dyHotelProductService.saveOrUpdateBatch(list);
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新",notes = "关联信息")
    @PostMapping("/update")
    public R update(@RequestBody DyHotelProductEntity dyHotelProduct){
		dyHotelProductService.updateById(dyHotelProduct);
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除关联",notes = "参数为Integer[] ids  数组")
    @DeleteMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		dyHotelProductService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
